package org.plex.core.imp;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.plex.core.ContextManager;
import org.plex.util.CheckUtil;
import com.adobe.idp.Context;
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.um.api.UMException;
import com.adobe.idp.um.api.UMLocalUtils;
import com.adobe.idp.um.api.infomodel.AuthResult;
import com.adobe.idp.um.credential.CredentialClient;
import com.adobe.idp.um.credential.CredentialPair;
import com.adobe.livecycle.usermanager.client.AuthenticationManagerServiceClient;

/**
 * 
 * @author 黄卫立
 * Context对像的管理类
 * 在默认的容器中已经包含了systemadmin为关键字的Context类，它提供了对系统函数底层调用的，用户认证权限
 */
public class ContextManagerImp extends HashMap<String,Context> implements
		ContextManager, Serializable {
	private static final long serialVersionUID = -8716223604435374364L;
	public static final String ADMINCONTEXT="systemadmin";
	private Hashtable<String,Context> _table;
	private static Context adminContext=null;
	private static String dsJndiName=null;
	
	public boolean containsKey(String key) {
		// TODO Auto-generated method stub
		return super.containsKey(key);
	}

	public boolean containsValue(Context value) {
		// TODO Auto-generated method stub
		return super.containsValue(value);
	}

	public Context get(String key) {
		// TODO Auto-generated method stub
		return super.get(key);
	}

	static {
		if (CheckUtil.isInVM()) {
			try {
				adminContext = UMLocalUtils.getSystemContext();
			} catch (UMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				adminContext = null;
			}
		}else
		{
			if(dsJndiName==null)
			{
				Properties properties=new Properties();
				try {
					properties.load(ContextManagerImp.class.getResourceAsStream("servers.properties"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					properties=null;
					e.printStackTrace();
				}
				dsJndiName=properties.get("jndi").toString();
				if(dsJndiName==null||dsJndiName.length()==0)
				{
					dsJndiName = "IDP_DS";
				}
			}
			
			CredentialPair credentialPair=CredentialClient.getCredentials(dsJndiName);
			ServiceClientFactory clientFactory=ServiceClientFactory.createInstance();
			AuthenticationManagerServiceClient authenticationManagerServiceClient=new AuthenticationManagerServiceClient(clientFactory);
			try {
				AuthResult authResult=authenticationManagerServiceClient.authenticate(credentialPair.getLoginId(),credentialPair.getPassword().getBytes("utf-8"));
				Context context=new Context();
				context.initPrincipal(authResult);
				AuthResult result=authenticationManagerServiceClient.getAuthResultOnBehalfOfUser("SuperAdmin", "DefaultDom", context);
				adminContext.initPrincipal(result);
			} catch (UMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public ContextManagerImp() {
		super();
		super.put("systemadmin", adminContext);

	}

	public ContextManagerImp(Hashtable<String, Context> table) {
	    super(table);
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}

	public void clear() {
		// TODO Auto-generated method stub
        super.clear();
	}

	public boolean containsKey(Object key) {
		// TODO Auto-generated method stub
		return super.containsKey(key);
	}

	public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return super.containsValue(value);
	}

	public Set<java.util.Map.Entry<String, Context>> entrySet() {
		// TODO Auto-generated method stub
		return super.entrySet();
	}

	public Context get(Object key) {
		// TODO Auto-generated method stub
		return super.get(key);
	}

	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return super.isEmpty();
	}

	public Set<String> keySet() {
		// TODO Auto-generated method stub
		return super.keySet();
	}


	public void putAll(Map<? extends String, ? extends Context> m) {
		super.putAll(m);
	}

	public Context remove(String key) {
		// TODO Auto-generated method stub
		return _table.remove(key);
	}

	public int size() {
		// TODO Auto-generated method stub
		return super.size();
	}

	public Collection<Context> values() {
		// TODO Auto-generated method stub
		return super.values();
	}

	public Context put(String key, Context value) {
		// TODO Auto-generated method stub
		return super.put(key, value);
	}

}
